import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';

import '../common/constants.dart';
import '../controllers/profile_controller.dart';
import '../widgets/mine_cartoon_list_view.dart';
import '../widgets/mine_pipe_list_view.dart';
import '../widgets/sliver_tab_bar_delegate.dart';
import '../widgets/sliver_tab_bar_view.dart';

class ProfilePage extends StatelessWidget {
  const ProfilePage({super.key});

  @override
  Widget build(BuildContext context) {
    final ProfileController controller = Get.put(ProfileController());

    return Obx(() {
      if (controller.user.value == null) {
        return Container(
          color: Colors.white,
          child: Center(
            child: LoadingAnimationWidget.progressiveDots(
              color: Constants.themeColor,
              size: 100,
            ),
          ),
        ); // Placeholder when user is null
      }
      return Stack(children: [
        CachedNetworkImage(
          width: double.infinity,
          fadeInDuration: const Duration(seconds: 0),
          fadeOutDuration: const Duration(seconds: 0),
          fit: BoxFit.cover,
          imageUrl: controller.user.value?.avatar ?? "",
          errorWidget: (_, url, object) {
            return Container(color: Colors.red);
          },
        ),
        BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 8.0, sigmaY: 8.0),
          child: Container(
            color: Colors.black.withAlpha(127),
          ),
        ),
        Scaffold(
          backgroundColor: Colors.transparent,
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.white,
          ),
          body: SafeArea(
            bottom: false,
            child: NestedScrollView(
              controller: controller.scrollController,
              headerSliverBuilder: (context, _) => [
                SliverToBoxAdapter(
                  child: Container(
                    padding: const EdgeInsets.only(top: 20),
                    child: Column(children: [
                      Stack(
                        children: [
                          ClipRRect(
                            borderRadius: BorderRadius.circular(36),
                            child: CachedNetworkImage(
                              height: 72,
                              width: 72,
                              fadeInDuration: const Duration(seconds: 0),
                              fadeOutDuration: const Duration(seconds: 0),
                              fit: BoxFit.cover,
                              imageUrl: controller.user.value?.avatar ?? "",
                              errorWidget: (_, url, object) {
                                return Container(color: Colors.grey[200]);
                              },
                            ),
                          ),
                          if (controller.userAvatarFrame.isNotEmpty)
                            Positioned(
                                left: -9,
                                top: -9,
                                child: CachedNetworkImage(
                                  height: 90,
                                  width: 90,
                                  imageUrl: controller.userAvatarFrame.value,
                                ))
                        ],
                      ),
                      const SizedBox(height: 10),
                      Text(controller.user.value?.nickname ?? "", style: const TextStyle(fontSize: 16, color: Colors.white, fontWeight: FontWeight.bold)),
                      const SizedBox(height: 4),
                      GestureDetector(
                        onTap: () async {
                          await Clipboard.setData(ClipboardData(text: controller.user.value?.uid ?? ""));
                          Fluttertoast.showToast(msg: "ID 已复制", gravity: ToastGravity.CENTER);
                        },
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [Text("ID: ${controller.user.value?.uid ?? ""}", style: const TextStyle(fontSize: 13, color: Colors.white54)), const SizedBox(width: 3), const Icon(Icons.copy, size: 12, color: Colors.white54)],
                        ),
                      ),
                      const SizedBox(height: 36),
                    ]),
                  ),
                ),
              ],
              body: ClipRRect(
                borderRadius: const BorderRadius.only(
                  topLeft: Radius.circular(16),
                  topRight: Radius.circular(16),
                ),
                child: ColoredBox(
                  color: Colors.white,
                  child: DefaultTabController(
                    length: controller.tabItems.length,
                    child: CustomScrollView(
                      physics: const NeverScrollableScrollPhysics(),
                      controller: controller.scrollController,
                      slivers: [
                        SliverPersistentHeader(
                          delegate: SliverTabBarDelegate(
                            tabBar: TabBar(
                              labelColor: Constants.themeColor,
                              labelStyle: const TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
                              unselectedLabelColor: Colors.grey,
                              unselectedLabelStyle: const TextStyle(fontSize: 15),
                              indicatorColor: Constants.themeColor,
                              dividerHeight: 0,
                              tabs: controller.tabItems.map((item) {
                                return Tab(text: item);
                              }).toList(),
                            ),
                            topRadius: Radius.zero,
                            backgroundColor: Colors.white,
                          ),
                          pinned: true,
                        ),
                        SliverTabBarView(
                          color: Colors.white,
                          children: [
                            MinePipeListView(uid: controller.uid),
                            MineCartoonListView(uid: controller.uid),
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        )
      ]);
    });
  }
}
